#include "sign_up.h"
#include "ui_sign_up.h"
#include "login.h"
sign_up::sign_up(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::sign_up)
{
    ui->setupUi(this);
    this->setWindowTitle(QObject::tr("注册界面"));
    QFont font("Microsoft YaHei",12,75);
    QFont font_line("宋体",12,30);
//####################
//创建一个Qcomobox部件显示账号类型那一行
//###################
    type=new QComboBox(this);
    //type->lineEdit()->setPlaceholderText(tr("选择账号类型"));
    type->addItem(QObject::tr("医护人员账号"));
    type->addItem(QObject::tr("患者账号"));
    type->setGeometry(150,20,200,30);
    type->setFont(font_line);
    //####################
    //创建一个Qlable部件
    //###################
    TYPE=new QLabel(this);
    TYPE->setText(QObject::tr("账号类型： "));
    TYPE->setGeometry(50,20,100,30);
    TYPE->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    TYPE->setFont(font);
   // TYPE->show();
//###################
//创建一个lable和lineedit显示姓名那一行
//######################
    name=new QLabel(this);
    name->setText(QObject::tr("姓名："));
    name->setGeometry(50,70,100,30);
    name->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    name->setFont(font);
    nameline=new QLineEdit(this);
    //QRegExp rx("[A-Za-z]");
    //nameline->setValidator(new QRegExpValidator(rx,this));
    nameline->setGeometry(150,70,200,30);
    nameline->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    nameline->setFont(font_line);
   // nameline->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    //nameline->setFont(font);

//####################
//创建一个Qcomobox部件和lable用来显示性别那一行
//###################
    sex= new QComboBox(this);//作为该界面的部件而不是窗口
    sex->addItem(QObject::tr("男"));
    sex->addItem(QObject::tr("女"));//添加选项
    //    sex->resize(100,30);使用下面的函数即可
    sex->setGeometry(150,120,200,30);//设置部件的位置，长宽高和起始坐标
    sex->setFont(font_line);
    //创建一个lable
    sex_lable=new QLabel(this);
    sex_lable->setText(QObject::tr("性别："));
    sex_lable->setGeometry(50,120,100,30);
    sex_lable->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    sex_lable->setFont(font);
//#######################
//显示身份证号那一行
//######################
    identity=new QLabel(this);
    identity->setText(QObject::tr("身份证号："));
    identity->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    identity->setGeometry(50,170,100,30);
    identity->setFont(font);
    Identity_line=new QLineEdit(this);
    Identity_line->setGeometry(150,170,200,30);
    Identity_line->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    Identity_line->setFont(font_line);
//#######################
//显示手机号那一行
//#####################
    phone_lable=new QLabel(this);
    phone_lable->setText(QObject::tr("手机号码："));
    phone_lable->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    phone_lable->setGeometry(50,220,100,30);
    phone_lable->setFont(font);
    phone_line=new QLineEdit(this);
    phone_line->setGeometry(150,220,200,30);
    phone_line->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    phone_line->setFont(font_line);
//##################
//密码那一行
//###################
    passwd_lable=new QLabel(this);
    passwd_lable->setText(QObject::tr("密码："));
    passwd_lable->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    passwd_lable->setGeometry(50,270,100,30);
    passwd_lable->setFont(font);
    passwd_line=new QLineEdit(this);
    passwd_line->setGeometry(150,270,200,30);
    passwd_line->setAlignment(Qt::AlignJustify|Qt::AlignCenter);
    passwd_line->setFont(font_line);
//#####################
//按钮功能
//#####################
    OK_ptn=new QPushButton(this);
    OK_ptn->setText(QObject::tr("确定"));
    OK_ptn->setGeometry(100,330,100,30);
    cencel_ptn=new QPushButton(this);
    cencel_ptn->setText(QObject::tr("取消"));
    cencel_ptn->setGeometry(300,330,100,30);
    this->setFixedSize(500,400);//设置窗口大小为固定大小
    connect(OK_ptn,&QPushButton::clicked,this,&sign_up::OK_ptn_clicked);
    connect(cencel_ptn,&QPushButton::clicked,this,&sign_up::cencer_ptn_clicked);
    connect(type,SIGNAL(currentIndexChanged(int)),this,SLOT(Change_view()));
    //lable1->show();
}

sign_up::~sign_up()
{
    delete ui;
}

int sign_up::Database_signup()
{


    if(type->currentIndex()==0)
        type_text=tr("worker");
    else if(type->currentIndex()==1)
        type_text=tr("customer");
    else
    {
        QMessageBox::warning(NULL,"ERROR","账户类型错误",QMessageBox::Ok);
        type->setFocus();
        return 0;
    }
   // qDebug()<<type_text;
    if(nameline->text()!=NULL)
        name_text=nameline->text();
    else
    {
        QMessageBox::warning(NULL,"ERROR","请输入姓名",QMessageBox::Ok);
        nameline->setFocus();
        return 0;
    }

    if(sex->currentIndex()==0)
        sex_text=tr("man");
    else if(sex->currentIndex()==1)
        sex_text=tr("woman");
    else
    {
        QMessageBox::warning(NULL,"ERROR","性别选项错误",QMessageBox::Ok);
        sex->setFocus();
        return 0;
    }

   // qDebug()<<sex_text;
    if(Identity_line->text()!=NULL)
        identity_text=Identity_line->text();
    else
    {
        QMessageBox::warning(NULL,"ERROR","身份证号输入错误",QMessageBox::Ok);
        Identity_line->setFocus();
        return 0;
    }

    if(phone_line->text()!=NULL)
        phone_text=phone_line->text();
    else
    {
        QMessageBox::warning(NULL,"ERROR","手机号错误",QMessageBox::Ok);
        phone_line->setFocus();
        return 0;
    }

    if(passwd_line->text()!=NULL)
        passwd_text=passwd_line->text();
    else
    {
        QMessageBox::warning(NULL,"ERROR","请输入密码",QMessageBox::Ok);
        passwd_line->setFocus();
        return 0;
    }

//##################################
//插入数据到数据表中并关闭连接
//#######################
     if(!MYSQL_Connect_signup())
     {
        QMessageBox::warning(NULL,"CONNECT DATABASE ERROR","连接数据库失败",QMessageBox::Ok);
        return 0;

     }
query=QSqlQuery(db);//对QSqlQuery::exec: database not open错误的解决方案，
    query.prepare("insert into Worker_Login_Hospital (type, Account, sex, Identity, phone, passwd, isValid) \
values (?,?,?,?,?,?,1)");
          query.bindValue(0,type_text);
          query.bindValue(1,name_text);
            query.bindValue(2,sex_text);
            query.bindValue(3,identity_text);
            query.bindValue(4,phone_text);
            query.bindValue(5,passwd_text);
           if(!query.exec())
           {
            QMessageBox::warning(NULL,"Insert to  table  Login_Hospital ERROR","数据表出错",QMessageBox::Ok);
            return 0;
           }

    db.close();
    QString CON_NAME=db.connectionName();
    db.removeDatabase(CON_NAME);
    return 1;

}

bool sign_up::MYSQL_Connect_signup()
{
     db=QSqlDatabase::addDatabase("QMYSQL","CON_SignUp");
      qDebug() << "MYSQL driver valid" << db.isValid();
               db.setHostName("2001:da8:270:2018:f816:3eff:fe1d:bb01");
               db.setPort(3306);
               db.setDatabaseName("MQTT_DATA");
               db.setUserName("root");
               db.setPassword("lama940225");
               if(!db.open())
               {
                   qDebug()<<"打开数据库失败！"<<db.lastError();
                   return false;
               }

               return true;
}



void sign_up::OK_ptn_clicked()
{
    if(!Database_signup())
    {
        qDebug()<<"输入出错，请重新输入";
//        nameline->clear();
//        Identity_line->clear();
//        phone_line->clear();
//        passwd_line->clear();
        db.close();
        QString CON_NAME=db.connectionName();
        db.removeDatabase(CON_NAME);
    }else
    {
        Login *login=new Login();
        this->close();
        login->show();
    }


}

void sign_up::cencer_ptn_clicked()
{
    Login *login=new Login();
    this->close();
    login->show();
}

void sign_up::Change_view()
{
    if(type->currentIndex()==0)
        type_text=tr("worker");
    else if(type->currentIndex()==1)
    {
        PSUp=new Patient_SignUp();
        this->close();
        PSUp->show();
    }
}
